//
//  ResourceViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ResourceViewController.h"
#import "GTPageController.h"
#import "ZJAdditions.h"
#import "RecommendViewController.h"
#import "HotViewController.h"
#import "ClassifyViewController.h"
#import "SpecialViewController.h"
#import "LPSearchViewController.h"
#import "ZJShareManager.h"
#import "ZJStudySpcialModel.h"
@interface ResourceViewController ()<GTMenuViewDelegate>
{
    GTPageController *_pageController;/**< 首页框架 */
    
}

/** 栏目数组 */
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ResourceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self netWorking];
    
    if (self.isDiscovery) {
        self.titlelabel.text = @"资源";
    }else{
        self.titlelabel.text = @"在线课堂";
    }
    
    self.rightButton.hidden = NO;
   //设置搜索按钮
    [self.rightButton setImage:[UIImage imageNamed:@"learn_nav_search"] forState:UIControlStateNormal];
    [self.rightButton setImage:[UIImage imageNamed:@"learn_nav_search"] forState:UIControlStateHighlighted];
    [self.rightButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:12];
    self.rightButton.titleEdgeInsets = UIEdgeInsetsMake(25, - self.rightButton.imageView.image.size.width, 0.0, 0.0);
    self.rightButton.imageEdgeInsets = UIEdgeInsetsMake(- 15, (self.rightButton.bounds.size.width - self.rightButton.imageView.image.size.width)/2, 0.0,(self.rightButton.bounds.size.width - self.rightButton.imageView.image.size.width)/2);

//     [self initUIWithPageController];
}


#pragma mark - 父类方法
- (void)action_leftButton:(UIButton *)sender{
   
    [[ToolClass getMainNavigationController] popViewControllerAnimated:YES];
    
}

- (void)action_rightButton:(UIButton *)sender{
    LPSearchViewController *vc = [LPSearchViewController new];
    vc.number = 0;
    [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
}


#pragma mark - 初始化控制器
- (void)initUIWithPageController{
    //框架
    _pageController = [self getDefaultController];
    if (self.isDiscovery) {
        _pageController.view.frame = CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight);
    }else{
        _pageController.view.frame = CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight - LP_TabBarHeight);
    }
    
    [self.view addSubview:_pageController.view];
    [self addChildViewController:_pageController];
    
}

- (GTPageController *)getDefaultController{
    RecommendViewController *vc = [[RecommendViewController alloc]init];
//    HotViewController *vc1 = [[HotViewController alloc] init];
//    SpecialViewController *vc2 = [[SpecialViewController alloc]init];
//    ClassifyViewController *vc3 = [[ClassifyViewController alloc] init];
    
//    NSArray *titles = @[@"推荐",@"热门",@"专题",@"分类"];
    __block NSMutableArray *titles = [NSMutableArray array];
    __block NSMutableArray *titlesWidth = [NSMutableArray array];
    __block NSMutableArray *viewControllers = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(ZJStudySpcialModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [titles addObject:obj.CLASS_NAME];
        [titlesWidth addObject:@([NSString zj_sizeOfString:obj.CLASS_NAME font:[UIFont systemFontOfSize:16] width:[UIScreen zj_screenWith]].width + 5 * [UIScreen zj_screenWScale])];
        [viewControllers addObject:[vc class]];
        
    }];
   
   
//    NSArray *viewControllers = @[[vc class],[vc1 class],[vc2 class],[vc3 class]];
    
    GTPageController *pageVC = [[GTPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    pageVC.lpNewsColmModelArray = self.dataArray;
    pageVC.pageAnimatable = YES;
    pageVC.itemsWidths = titlesWidth;
    pageVC.postNotification = YES;
    pageVC.isStudy = !self.isDiscovery;
    pageVC.menuBGColor = [UIColor whiteColor];
    pageVC.menuViewStyle = GTMenuViewStyleLine;
    pageVC.titleColorNormal = [UIColor zj_colorWithHexString:@"626262"];
    
    return pageVC;
}


#pragma mark - 网络请求
- (void)netWorking{
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getSt] Parameter:nil Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJStudySpcialModel class] json:[object objectForKey:@"ST_CLASS"]] mutableCopy];
            [weakSelf initUIWithPageController];

        }else{
            [YJProgressHUD showMsgWithoutView:@"加载失败"];
        }
        
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"加载失败"];
    }];
}



- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}
@end
